package com.gipson.amc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.ResponseMessage;
import com.gipson.amc.config.annotation.RequireClientToken;
import com.gipson.amc.entity.Company;
import com.gipson.amc.entity.PostGroup;
import com.gipson.amc.entity.User;
import com.gipson.amc.model.TreeNode;
import com.gipson.amc.model.request.ClientTokenRequest;
import com.gipson.amc.model.request.LoginRequest;
import com.gipson.amc.model.request.UserRequest;
import com.gipson.amc.service.basicdata.*;
import com.gipson.amc.utils.strprocess.ArrayUtil;
import com.gipson.amc.utils.strprocess.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping(value = "/auth")
public class AuthController {

	@Autowired
	private UserService userService;

	@Autowired
	private ApplicationService applicationService;

	@Autowired
	private PostGroupService postGroupService;

	@Resource
	private UserPostGroupService userPostGroupService;
	
	@Resource
	private CompanyService companyService;
	
	
	
	
	@RequestMapping("/login")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage login(HttpServletRequest request, @RequestBody LoginRequest loginRequest) {
		if (StringUtils.isBlank(loginRequest.getApplicationId())) {
			loginRequest.setApplicationId(request.getHeader("X-APPID"));
		}
		return userService.authCredentials(loginRequest);
	}


	@RequestMapping("/listUserByIds")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage listUserByIds(@RequestBody List<String> ids) {
		return userService.listUserByIds(ids);
	}

	/**
	 * 根据userId获取user信息。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-20
	 */
	@RequestMapping("/getUserById")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage getUserById(@RequestBody String userId) {
		//解析传来的参数
		if (StrUtil.isEmpty(userId)) {
			return ResponseMessage.error("userId为空!");
		}

		//根据userId获取user信息。
		User user = userService.findUserByUserId(userId);
		if (user == null) {
			return ResponseMessage.error("未找到userId对应的user信息!");
		}
		//StrUtil.prettyPrint("getUserById().user", user);
		return ResponseMessage.ok(user);
	}

	/**
	 * 根据user获取user信息。
	 * 传入参数 name, userCode。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-20
	 */
	@RequestMapping("/getUserBy")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage getUserBy(@RequestBody User user) {
		/**
		 * 根据user获取user列表。
		 */
		user.setDelFlag(0);
		List<User> userList = userService.getUserByUser(user);
		return ResponseMessage.ok(userList);
	}

	/**
	 * 分页获取user信息。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-20
	 */
	@RequestMapping("/getUserByPage")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage getUserByPage(@RequestBody UserRequest userRequest) {
		//StrUtil.prettyPrint("getUserByPage().userRequest", userRequest);

		//分页获取user信息。
		Page<User> page = userService.getUserByPage(userRequest);
		return ResponseMessage.ok(page);
	}


	/**
	 * 根据userIdList获取user列表。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-20
	 */
	@RequestMapping("/getUserByIdList")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage getUserByIdList(@RequestBody UserRequest userRequest) {
		//StrUtil.prettyPrintWithoutNullValue("getUserByIdList().userRequest", userRequest);
		//解析传来的参数
		if (ArrayUtil.isEmpty(userRequest.getUserIdList())) {
			return ResponseMessage.error("userIdList为空!");
		}

		//根据userIdList获取user列表。
		List<User> userList = userService.getUserByIdList(userRequest.getUserIdList());
		return ResponseMessage.ok(userList);
	}


	/*****
	 * 获取岗位树wcm
	 * @return
	 */
	@RequestMapping("/getPostGroupTree")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage getPostGroupTree(@RequestBody String companyCode) {
		List<TreeNode> treeList = new ArrayList<>();
		List<PostGroup> postGroupList = postGroupService.getPostByCompanyCode(companyCode);
		for (PostGroup postGroup : postGroupList) {
			TreeNode treeNode = new TreeNode();
			treeNode.setId(postGroup.getId());
			treeNode.setName(postGroup.getPostName());
			treeNode.setPId(StringUtils.isBlank(postGroup.getParentId()) ? postGroup.getId() : postGroup.getParentId());
			treeNode.setValue(postGroup.getPostCode());
			treeNode.setIcon(StringUtils.isBlank(postGroup.getParentId()) ? "/static/imgs/tree/firtree.png" : "/static/imgs/tree/depart-tree.png");
			if (StringUtils.isNotBlank(postGroup.getParentId())) {
				treeNode.setType(1);
				treeNode.setOpen(false);
			} else {
				treeNode.setType(0);
				treeNode.setOpen(true);
			}
			treeList.add(treeNode);
		}
		return ResponseMessage.ok(treeList);
	}

	/**
	 * 根据userId获取岗位列表。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-20
	 */
	@RequestMapping("/getPostGroupByUserId")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage getPostGroupByUserId(@RequestBody UserRequest userRequest) {
		//StrUtil.prettyPrint("getPostGroupByUserId().userRequest", userRequest);

		//解析传来的参数
		if (StrUtil.isEmpty(userRequest.getId())) {
			return ResponseMessage.error("id为空!");
		}

		//根据userId获取postGroup列表
		List<PostGroup> postGroupList = userPostGroupService.getPostGroupByUserId(userRequest.getId());
		return ResponseMessage.ok(postGroupList);
	}

	/**
	 * 根据postGroupId获取postGroup信息。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-20
	 */
	@RequestMapping("/getPostGroupById")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage getPostGroupById(@RequestBody PostGroup postGroup) {
		//StrUtil.prettyPrint("getPostGroupById().postGroup", postGroup);

		//解析传来的参数
		if (StrUtil.isEmpty(postGroup.getPostGroupId())) {
			return ResponseMessage.error("postGroupId为空!");
		}

		//根据postGroupId获取postGroup信息。
		PostGroup postGroup2 = postGroupService.getPostGroupFullById(postGroup.getPostGroupId());
		return ResponseMessage.ok(postGroup2);
	}


	/**
	 * 根据postIdList获取postGroup信息。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-20
	 */
	@RequestMapping("/getPostGroupByIdList")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage getPostGroupByIdList(@RequestBody PostGroup postGroup) {
		//StrUtil.prettyPrint("getPostGroupByIdList().postGroup", postGroup);
		//解析传来的参数
		if (ArrayUtil.isEmpty(postGroup.getPostIdList())) {
			return ResponseMessage.error("postIdList为空!");
		}
		//根据postIdList获取postGroup列表
		List<PostGroup> postGroupList = postGroupService.getPostGroupFullByIdList(postGroup.getPostIdList());
		return ResponseMessage.ok(postGroupList);
	}
	
	/**
	 * 获取company列表。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-20
	 */
	@RequestMapping("/getCompanyList")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage getCompanyList() {
		List<Company> companyList = companyService.getCompany();
		return ResponseMessage.ok(companyList);
	}
	
	/**
	 * @description 修改用户密码
	 * @param
	 * @author: xsyuan 2020/5/21 15:56
	 * @return
	 */
	@RequestMapping("/editByPassword")
	@RequireClientToken
	@ResponseBody
	public ResponseMessage editByPassword(@RequestBody User user){
		return userService.updateByPassword(user);
	}

	@RequestMapping("/getClientToken")
	@ResponseBody
	public ResponseMessage getClientToken(@RequestBody ClientTokenRequest clientTokenRequest) {
		return applicationService.getClientToken(clientTokenRequest);
	}

}
